#include "utils.h"
#include <string.h>
#include "cfg.h"
#include <hiredis/hiredis.h>
#include <stdlib.h>
#include "make_log.h"
#include <cJSON.h>
#include <ctype.h>

int parse_get_params(const char* query, const char* key, char* value, int* value_len_p)
{
    char* tmp = NULL;
    char* end = NULL;
    int value_len = 0;

    tmp = strstr(query, key);
    if(tmp == NULL)
    {
        return -1;
    }

    tmp += strlen(key);
    tmp++;

    end = tmp;
    while('\0' != *end && '#' != *end && '&' != *end)
    {
        end++;
    }

    value_len = end - tmp;

    strncpy(value, tmp, value_len);
    value[value_len] = '\0';

    if(value_len_p != NULL)
    {
        *value_len_p = value_len;
    }

    return 0;
}

int verify_token(char* user, char* token)
{
    int ret = 0;

    char redis_ip[32] = {0};
    char redis_port[32] = {0};

    get_cfg_value(CFG_PATH, "redis", "ip", redis_ip);
    get_cfg_value(CFG_PATH, "redis", "port", redis_port);

    redisReply* reply = NULL;
    redisContext* context = NULL;

    do
    {
        context = redisConnect(redis_ip, atoi(redis_port));
        if(context == NULL || context->err)
        {
            if(context)
            {
                LOG("%s\n", context->errstr);
            }
            else
            {
                LOG("redisConnect error\n");
            }
            ret = -1;
            break;
        }

        reply = redisCommand(context, "get %s", user);
        LOG("user = %s\n", user);
        if(reply == NULL)
        {
            ret = -1;
            LOG("Error %s\n", context->errstr);
            break;
        }

        if(strcmp(reply->str, token) != 0)
        {
            ret = -1;
            LOG("error replay->str\n");
            break;
        }
    }while(0);

    if(reply != NULL)
    {
        freeReplyObject(reply);
    }

    if(context != NULL)
    {
        redisFree(context);
    }
    
    return ret;
}

char* return_status(char* status)
{
    char* out = NULL;
    cJSON* root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "code", status);
    out = cJSON_Print(root);
    cJSON_Delete(root); 
    return out;
}

int trim_space(char* inbuf)
{
    int i = 0;
    int j = strlen(inbuf) - 1;

    char* str = inbuf;
    int count = 0;

    if(str == NULL)
    {
        return -1;
    }

    while(isspace(str[i]) && str[i] != '\0')
    {
        i++;
    }

    while(isspace(str[j]) && j > i)
    {
        j--;
    }

    count = j - i + 1;

    strncpy(inbuf, str + i, count);

    inbuf[count] = '\0';
    
    return 0;
}

int get_file_suffix(const char* file_name, char* suffix)
{
    const char* p = file_name;
    int len = 0;
    const char* q = NULL;
    const char* k = NULL;

    if(p == NULL)
    {
        return -1;
    }

    q = p; 

    while(*q != '\0')
    {
        ++q;
    }

    k = q;
    while(*k != '.' && k != p)
    {
        --k;
    }

    if(*k == '.')
    {
        ++k;
        len = q - k;

        if(len != 0)
        {
            strncpy(suffix, k, len);
            suffix[len] = '\0';
        }
        else
        {
            strncpy(suffix, "null", 5);
        }
        LOG("res： %s\n", suffix);
    }
    else
    {
        strncpy(suffix, "null", 5);
    }

    return 0;
}

int read_cfg(char* mysql_user, char* mysql_pwd, char* mysql_db)
{
    int ret = 0;

    do
    {
        if(get_cfg_value(CFG_PATH, "mysql", "user", mysql_user) != 0)
        {
            ret = -1;
            LOG("get_cfg_value() error\n");
            break;
        }

        if(get_cfg_value(CFG_PATH, "mysql", "password", mysql_pwd) != 0)
        {
            ret = -1;
            LOG("get_cfg_value() error\n");
            break;
        }

        if(get_cfg_value(CFG_PATH, "mysql", "database", mysql_db) != 0)
        {
            ret = -1;
            LOG("get_cfg_value() error\n");
            break;
        }

        LOG("mysql:[user=%s,pwd=%s,database=%s]", mysql_user, mysql_pwd, mysql_db);
    }while(0);

    return ret;
}